#include <unistd.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <string.h>  
#include <errno.h>  
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>

#define MAX_TEXT 512

struct msg_struct
{
    long msg_type;
    char msg_text[MAX_TEXT];
}msg_st;


int main()
{
     int                     msgid = -1;
     int                     starting = 1;    
     long int                msgtype = 0;
     struct msg_struct       data;
     char                    buffer[BUFSIZ];


     msgid = msgget((key_t)1234, 0666 | IPC_CREAT);  //建立消息队列
     
     if(msgid < 0 )
     { 
           printf("creta msgget failure\n");
           return -1;      
     } 
     printf("creat msgget successful\n");

     while(starting)        //输入数据
     {
           printf("请输入消息：\n");
           fgets(buffer, BUFSIZ, stdin);    
           data.msg_type = 1;  
          
           // 向队列发消息 
           strcpy(data.msg_text, buffer); 


            if(msgsnd(msgid,(void*)&data,MAX_TEXT , 0) <0 )
            {
                  printf("msgsnd errno :%d\n", errno);
                  return -2;
            }   
            
            // 输入end时则结束对话
            if(strncmp(buffer, "end", 3) == 0)
                  starting = 0;

             sleep(1); 
     }
     
   
     return 0;
}
